<%@ page language="java" pageEncoding="UTF-8"%>
<!--分页查询共用的页面-->
<%@ include file="/common/common.jsp"%>

<!--easyui的主题css-->


<script type="text/javascript" src="<%=jsPath%>/jquery/jquery.timers.js"></script><!--定时器-->
<script type="text/javascript" src="<%=jsPath%>/terminalCommand.js?v=1"></script><!--命令结果查询-->
<!--[if lt IE 9]>
<script src="<%=jsPath%>/json2.js"></script>
<![endif]-->


<style>
	input
	{
		height:20px !important;
	}
</style>

<script>
    /*
	修改代码：
	1.新定义变量TerminalCommmand，为命令支持变量
	2.重定义id：
	原：null		现：tvpCommandMsg
	原：btnQuery		现：btnQuery
	原：		现：
	*/

	var tc2AvChanelList = new TerminalCommand2();
     var timerName = "avChannelListTimer";
	///////////////////////////////////////////////////////////////


    $(window).ready(function () {
        tc2AvChanelList.messageDiv = $("#tvpCommandMsg");
		tc2AvChanelList.timerName = timerName;
		tc2AvChanelList.commandButton = $("#btnQuery");


        $("#entityForm").validate(); //初始化验证信息

        var defaultChannelId = "1";//默认选中
        $("#channel").fillVehicleChannels({vehicleId:"${vehicleId}",selectedValue:defaultChannelId});

        $(".sendjson").click(function()
        {
            //var gridData = eTable.getJsonData();
            //alert(gridData);
            //$("#eventList").val(gridData);


        });

        createEditGrid();
    });
    //当查询终端参数命令执行成功时
    function onQueryCommandSuccess(result){
        if (result.success) {
            var p = result.data[0];
            var strArray = p.value.split("|");
            var i = 0;
            var rowDataList = [];
            for(var i=0; i < strArray.length; i++)
            {
                var t = strArray[i].split(",");
                var rowData = {};
                var m = 0;
                rowData["physicalChannelId"] = t[m++];
                rowData["logicalChannelId"] = t[m++];
                rowData["channelType"] = t[m++];
                rowData["panelType"] = t[m++];
                rowDataList.push(rowData);
            }
            $("#dg").datagrid("loadData",{rows:rowDataList});
            //var rows = $dg.datagrid('getRows');
            for(var m = 0; m < rowDataList.length ; m++)
            $("#dg").datagrid('beginEdit', m);
        }

    }
    /**
     * 下发参数查询指令
     */
    function sendParamQueryCommand()
    {
        operation = "query";
        var url = "<%=ApplicationPath%>/videoTerminalParam/sendParamQueryCommand.action";
        var param = {paramId : 0x0076,vehicleId:${vehicleId}};
        $.getJSON(url, param, function(result)
        {
            if (result.success) {
                var commandId = result.data; //下发成功后，获取到命令Id
                tc2AvChanelList.commandButton = $("#btnQuery");
                var url = "<%=ApplicationPath%>/terminalParam/queryTerminalParam.action";
                tc2AvChanelList.times=190;
                tc2AvChanelList.startQueryResult(commandId,url,onQueryCommandSuccess);//命令下发成功,根据命令id,开始尝试获取检索结果

            }else {
                $("#aclCommandMsg").html("提交失败! 错误原因：" + (result.message ? result.message : result.Data));
                //停止所有的在$('body')上定时器
                $('body').stopTime (timerName);
            }
        });
    }


    /**
     * 提交设置参数
     */
    function doSubmit()
    {
        var vehicleId = "${vehicleId}";
        endEdit();
        var $dg = $("#dg");
        var data= $dg.datagrid("getData").rows;
        if(data.length ==0)
        {
            $("#aclCommandMsg").html("请录入通道列表");
            return;
        }

        var effectRow = {vehicleId:vehicleId};
        effectRow["inserted"] = JSON.stringify(data);

        var url = "<%=ApplicationPath%>/videoTerminalParam/configAvChannelListParam.action";
        $.get(url, effectRow, function(result) {
            if(result.success){
                var commandId = result.data; //下发成功后，获取到命令Id
                tc2AvChanelList.commandButton = $("#btnConfig");
                tc2AvChanelList.startQueryResult(commandId);//命令下发成功,根据命令id,开始尝试获取检索结果
                $dg.datagrid('acceptChanges');
            }else
            {
                $("#aclCommandMsg").html("提交失败! 错误原因：" + (result.message ? result.message : result.Data));
                //停止所有的在$('body')上定时器
                $('body').stopTime (timerName);
            }
        }, "JSON").error(function() {

            //$(".commandMsg").html("提交失败! 错误原因：" + (result.message ? result.message : result.Data));
            //停止所有的在$('body')上定时器
            $('body').stopTime (timerName);
        });
        //}
    }
</script>
<script>
    function  createEditGrid()
    {

        var channelType = [{"value":"0","text":"音视频"},{"value":"1","text":"音频"},{"value":"2","text":"视频"}];
        var panelType = [{"value":"0","text":"未连接"},{"value":"1","text":"已连接"}];
        var $dg = $("#dg");
        $dg.datagrid({
            url : "servlet/list",
            fitColumns: true,
            rownumbers:true,
            columns : [ [ {
                field : 'physicalChannelId',
                title : '物理通道号',
                width : 100,
                editor : "validatebox"
            }, {
                field : 'logicalChannelId',
                title : '逻辑通道号',
                width : 200,
                editor : "validatebox"
            } , {
                field : 'channelType',
                title : '通道类型',
                width : 200,
                editor:{
                    type: 'combobox',
                    options: {
                        data: channelType,
                        valueField: "value",
                        textField: "text",
                        editable: false,
                        panelHeight:70,
                        required: true
                    }
                }} ,
				{
                field : 'panelType',
                title : '是否连接云台',
                width : 200,
                editor:{
                    type: 'combobox',
                    options: {
                        data: panelType,
                        valueField: "value",
                        textField: "text",
                        editable: false,
                        panelHeight:70,
                        required: true
                    }
                }}
            ] ],
            toolbar : [ {
                text : "添加",
                iconCls : "icon-add",
                handler : function() {
                    $dg.datagrid('appendRow', {});
                    var rows = $dg.datagrid('getRows');
                    $dg.datagrid('beginEdit', rows.length - 1);
                }
            }, {
                text : "编辑",
                iconCls : "icon-edit",
                handler : function() {
                    var row = $dg.datagrid('getSelected');
                    if (row) {
                        var rowIndex = $dg.datagrid('getRowIndex', row);
                        $dg.datagrid('beginEdit', rowIndex);
                    }
                }
            }, {
                text : "删除",
                iconCls : "icon-remove",
                handler : function() {
                    var row = $dg.datagrid('getSelected');
                    if (row) {
                        var rowIndex = $dg.datagrid('getRowIndex', row);
                        $dg.datagrid('deleteRow', rowIndex);
                    }
                }
            }]
        });
    }

    function endEdit(){
        var $dg = $("#dg");
        var rows = $dg.datagrid('getRows');
        for ( var i = 0; i < rows.length; i++) {
            $dg.datagrid('endEdit', i);
        }
    }

</script>
<BODY>
<div style="padding:5px;">
    <button id="btnConfig" onclick="doSubmit();"  class="button green medium sendjson" ><i class="fa fa-cog" style="margin-right:3px;"></i> 设置参数</button>

    <button id="btnQuery" onclick="sendParamQueryCommand();" type="button" class="button blue medium " ><i class="fa fa-reply-all" style="margin-right:3px;"></i> 查询参数</button>


    <span id="aclCommandMsg" class="commandMsg"></span>

</div>
<table id="dg" title="音视频通道列表"></table>


</BODY>
</HTML>
